package com.axsoft.jobimp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

/**
 * 
 * @author RankoR
 *
 * Socket for OBIMP
 */
public class OSocket {
	
	private Socket mSocket;
	private OutputStream mOutStream;
	private InputStream mInStream;
	
	public OSocket(boolean pSSL) {
		if (! pSSL) {
			mSocket = new Socket();
		}
	}
	
	/**
	 * Connect to OBIMP server via TCP/IP
	 */
	public void connect(final String pServer, int pPort, int pTimeout) throws IOException {
		final InetAddress addr = InetAddress.getByName(pServer);
		final SocketAddress sa = new InetSocketAddress(addr, pPort);
		mSocket.connect(sa, pTimeout);
		mOutStream = mSocket.getOutputStream();
		mInStream = mSocket.getInputStream();
	}
	
	/**
	 * Write stream to the socket
	 */
	public void write(final Byte[] pStream) throws IOException {
//		mOutStream.write(pStream	);
	}
	
	/**
	 * Read stream from the socket
	 * @throws IOException 
	 */
	public final byte[] read(int pLength) throws IOException {
		if (pLength <= 0 || pLength > 4096) {
			pLength = 4096;
		}
		
		final byte[] buffer = new byte[pLength];
		
		mInStream.read(buffer, 0, pLength);
		return buffer;
	}
	
}
